/**
 * Title : jFamilyTree
 * Description: jFamilyTree
 * About: Designed and rewritten by Andreas Lundgren, based on the Treebolic application suite
 * Copyright:	(c) 2001-2008
 * Terms of use:see license agreement at http://treebolic.sourceforge.net/en/license.htm
 * Author: Bernard Bou
 * Company: bsys
 */
package jFamilyTree.commons;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

/**
 * Component to be used as tabComponent; Contains a JLabel to show the text and a JButton to close the tab it belongs to. Copyright (c) 1995 - 2008 Sun
 * Microsystems, Inc. All rights reserved.
 */
@SuppressWarnings("serial")
public class ButtonTabComponent extends JPanel
{
	/**
	 * Default icon
	 */
	static private Icon theDefaultIcon = new ImageIcon(ButtonTabComponent.class.getResource("images/close_default.png")); //$NON-NLS-1$

	/**
	 * Rollover icon
	 */
	static private Icon theRolloverIcon = new ImageIcon(ButtonTabComponent.class.getResource("images/close_rollover.png")); //$NON-NLS-1$

	/**
	 * Pressed icon
	 */
	static private Icon thePressedIcon = new ImageIcon(ButtonTabComponent.class.getResource("images/close_pressed.png")); //$NON-NLS-1$

	/**
	 * Reference to tabbed pane
	 */
	private final JTabbedPane theTabbedPane;

	/**
	 * Constructor
	 * 
	 * @param thisPane
	 *        tabbed pane
	 */
	public ButtonTabComponent(final JTabbedPane thisPane)
	{
		// unset default FlowLayout' gaps
		super(new FlowLayout(FlowLayout.LEFT, 0, 0));

		if (thisPane == null)
			throw new NullPointerException("TabbedPane is null"); //$NON-NLS-1$
		theTabbedPane = thisPane;

		// L A B E L

		// make label read titles from JTabbedPane
		JLabel thisLabel = new JLabel()
		{
			public String getText()
			{
				int thisIndex = thisPane.indexOfTabComponent(ButtonTabComponent.this);
				if (thisIndex != -1)
					return thisPane.getTitleAt(thisIndex);
				return null;
			}
		};

		// add more space between the label and the button
		thisLabel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 5));

		// B U T T O N

		JButton thisButton = new JButton();

		// tooltip
		thisButton.setToolTipText(Messages.getString("ButtonTabComponent.0")); //$NON-NLS-1$

		// transparent
		thisButton.setContentAreaFilled(false);

		// no need to be focusable
		thisButton.setFocusable(false);

		// border
		thisButton.setBorder(BorderFactory.createEtchedBorder());
		thisButton.setBorderPainted(false);

		// icon
		thisButton.setRolloverEnabled(true);
		thisButton.setIcon(theDefaultIcon);
		thisButton.setRolloverIcon(theRolloverIcon);
		thisButton.setPressedIcon(thePressedIcon);

		// close the proper tab by clicking the button
		thisButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent thisEvent)
			{
				int thisIndex = theTabbedPane.indexOfTabComponent(ButtonTabComponent.this);
				if (thisIndex != -1)
					theTabbedPane.remove(thisIndex);
			}
		});

		// C O M P O N E N T

		// add more space to the top of the component
		setBorder(BorderFactory.createEmptyBorder(2, 0, 0, 0));

		// opaque
		setOpaque(false);

		// assemble
		add(thisLabel);
		add(thisButton);
	}
}
